<?php

namespace App\Http\Controllers\Api\V1\Admin;


use App\Http\Resources\BooksResource;
use App\Models\BookChapters;
use App\Models\Books;
use App\Models\BookType;
use App\Models\Category;
use App\Models\New_Category;
use App\Models\News;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;

class BookChaptersController extends BaseController
{
    public function index(BookChapters $chapters, Request $request)
    {
        $model = $chapters->newQuery();
        //获取单本目录
        if ($request->has('keyword') && !empty($request->keyword)) {
            $model->where('book_id', $request->keyword);
        }
        if ($request->has('status') && !empty($request->status)) {
            $model->where('status', $request->status);
        } else {
            $model->whereNotIn('status', [0]);
        }
        //排序
        if ($request->has('sort') && !empty($request->sort) && $request->sort == 1) {
            $model->orderBy('sort', 'asc');
        } else if ($request->has('sort') && !empty($request->sort) && $request->sort == 2) {
            $model->orderBy('sort', 'desc');
        }
        if (empty($request->input())) {
            $list = [];
        } else {
            $model->with('book');
            $list = $model->paginate();
        }
        foreach ($list as &$value) {
            $images = array_filter(explode(',', $value['imgs']));
            $image_list = [];
            foreach ($images as $image) {
                $image_list[] = dealUrl($image);
            }
            $value['imgs'] = $images;
            $value['images_list'] = $image_list;
        }
        return $this->success('成功', $list);
    }

    public function store(Request $request)
    {
        if (empty($request->name)) {
            return $this->error('章节名不能为空');
        }
        $model = new BookChapters();
        $model->name = $request->name;
        $model->content = $request->input('content');//内容
        $model->sort = $request->sort;
        $model->book_id = $request->book_id;
        $model->description = $request->description;
        if (!empty($request->images) && count($request->images) && is_array($request->images)) {
            $model->imgs = implode(',', $request->images);
        }
        if ($request->has('videos')) {
            $model->videos = $request->implode(',', $request->videos);
        }
        $model->status = 2;
        $result = $model->save();
        if ($result) {
            return $this->success('添加成功');
        } else {
            return $this->error('添加失败');
        }
    }

    public function update(Request $request, $chapter)
    {
        if (empty($request->name)) {
            return $this->error('章节名不能为空');
        }
        $model = BookChapters::find($chapter);
        $model->name = $request->name;
        $model->content = $request->input('content');//内容
        $model->sort = $request->sort;
        $model->book_id = $request->id;
        $model->description = $request->description;
        if (!empty($request->images) && is_array($request->images) && count($request->images)) {
            $model->imgs = implode(',', $request->images);
        }
        if ($request->has('videos') && is_array($request->videos) && count($request->videos)) {
            $model->videos = $request->implode(',', $request->videos);
        }
        $result = $model->save();
        if ($result) {
            return $this->success('修改成功', $result);
        } else {
            return $this->error('修改失败');
        }
    }

    public function destroy($chapter)
    {
        $result = BookChapters::where('id', $chapter)->update(['status' => 0]);
        if ($result) {
            return $this->success('删除成功', $result);
        } else {
            return $this->error('删除失败');
        }
    }

    public function batchDisable(Request $request)
    {
        $input = $request->toArray();
        if (!in_array($input['status'], [0, 1, 2])) {
            return $this->error('状态不正确');
        }
        $result = BookChapters::whereIn('id', $input['selection'])->update(['status' => $input['status']]);
        if ($result) {
            return $this->success('保存成功');
        } else {
            return $this->error('保存失败');
        }
    }

    public function getModels()
    {
        return $this->success('成功', Books::MODULE);
    }
}
